Inheritance in Python
1480
26-Jun-2018
Aryan Kumar
01-May-2023In Python, inheritance is the process of creating a new class that is a modified version of an existing class. The new class, called the child class or subclass, inherits the properties and methods of the existing class, called the parent class or superclass.
To create a subclass in Python, you define a new class and specify the name of the parent class in parentheses after the class name:
In this example, we define a parent class called ParentClass with two methods method1() and method2(). We then define a child class called ChildClass that inherits from ParentClass using the syntax class ChildClass(ParentClass):.
The ChildClass overrides the implementation of method2() from ParentClass by defining its own implementation of the method. It does not modify the implementation of method1() from the parent class.
To create an instance of the ChildClass, you simply call the constructor of the class:
In Python, you can also use the super() function to call a method in the parent class from within a method in the child class:
In this example, the method2() of the ChildClass calls the parent class implementation of the method using the super() function before printing its own message.
Prakash nidhi Verma
26-Jun-2018Inheritance allows for one class to gain all the attributes and methods of another class. code reusability increase in Inheritance. it makes easier to create and maintain an application. The classfrom which we are inheriting is known as super-class and the class that is inherited is called a child class.
Type of Inheritance in Python :
- Single Inheritance
- Multi-level inheritance
- Hierarchical inheritance
- Multiple inheritance